package pl.edu.pw.elka.jtrzasko.proz.warcaby.figures;

import java.io.Serializable;

import pl.edu.pw.elka.jtrzasko.proz.warcaby.model.Board;
import pl.edu.pw.elka.jtrzasko.proz.warcaby.model.Position;

public class Pawn extends Figure implements Serializable
{

	public Pawn(Position position, FigureColor color)
	{
		super(color, position);
	}

	public boolean isMoveValid(Position p2, Board board)
	{
		if (((position.getXdiff(p2) == 1
				&& board.isFieldEmpty(p2) 
				&& board.getFigure(position).getColor() == FigureColor.WHITE) ||
				(position.getXdiff(p2) == -1
				&& board.isFieldEmpty(p2) 
				&& board.getFigure(position).getColor() == FigureColor.BLACK))
				&& Math.abs(position.getYdiff(p2)) == 1)
			return true;
		return false;
	}

	public boolean isBeatingValid(Position p2, Board board)
	{
		if (Math.abs(position.getXdiff(p2)) == 2
				&& Math.abs(position.getYdiff(p2)) == 2
				&& board.isFieldEmpty(p2)
				&& !board.isFieldEmpty(new Position(position, p2))
				&& board.getFigure((new Position(position, p2))).getColor()
						.nextColor() == board.getFigure(position).getColor())
			return true;
		return false;
	}

	public boolean isPromotionValid(Board board)
	{
		if (position.getX() == 0
				&& board.getFigure(position).getColor() == FigureColor.WHITE)
			return true;
		if (position.getX() == (Board.LINE_MAX - 1)
				&& board.getFigure(position).getColor() == FigureColor.BLACK)
			return true;
		return false;
	}
}
